<?php

namespace app\agent\controller;

use app\common\controller\Adminbase;
use app\model\DistributionSetting;
use app\model\AgentAudit;
use app\service\AgentService;

class Shop extends Adminbase
{
    public function index()
    {
        $model = 'AgentAudit';

        $agent = AgentService::getAgentInfo(UID);
        if ($this->request->isAjax()) {
            $map      = $this->buildParamsMap();
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : 20;

            $query       = AgentAudit::where($map)
                ->where('agent_id', $agent['id']);
            $count_query = clone $query;

            $list  = $query
                ->field('*')
                ->with(['shop'])
                ->withAttr('status', function ($value) {
                    $text = [0 => '未审核', 1 => '已审核', 2 => '驳回'];
                    return $text[$value];
                })
                ->withAttr('settle_amount', function ($value) {
                    return $value > 0 ? $value / 100 : 0;
                })
                ->withAttr('product_number', function ($value) {
                    return $value ?: 0;
                })
                ->withAttr('approved_time', function ($value) {
                    return (is_numeric($value) && $value > 0) ? date('Y-m-d H:i:s', $value) : '';
                })
                ->page($page, $pageSize)
                ->select();
            $total = $count_query->count();

            $result = ["code" => 0, "count" => $total, "data" => $list];
            return json($result);
        }

        //生成列表数据
        $builder = new \app\common\controller\ListBuilder();
        $builder
            ->setLayerBtn([
                ['title' => '添加', 'url' => url('form'), 'area' => "['55vw', '80vh']"]
            ])
            ->setDelBtn(false)
            ->setAddBtn(false)
            ->setDelAllBtn(false)
            ->setDataUrl()
            ->setFormUrl('')
            ->setLimit()
            ->setModel($model)
            ->addTableCheckBox()
            ->setSwitchTpl('switchTpl', 'status')
            ->addTableColumn('id', 'ID', null, null, true)
            ->addTableColumn('name', '微信小店名称')
            ->addTableColumn('appid', '微信小店ID')
            ->addTableColumn('product_number', '合作商品数')
            ->addTableColumn('settle_amount', '成交额')
            ->addTableColumn('approved_time', '合作开始时间')
            ->addTableColumn('audit_remark', '备注')
            ->addTableColumn('status', '审核状态')
            ->returnForm();

        $value = DistributionSetting::where('uuid', 'partner')->value('value');
        if ($value) {
            $value = json_decode($value, true);
            if (!empty($value['shop_url'])) {
                $this->assign('text', "<a href='{$value['shop_url']}' target='_blank' style='color: red'>如何添加微信小店</a>");
            }
        }
        $this->assign('module', 'app');
        return $this->fetch('admin@common/lists');
    }

    public function form()
    {
        $model = 'AgentAudit';

        $info = [];
        if ($this->request->isPost()) {
            $agent = AgentService::getAgentInfo(UID);
            $data  = $this->request->post();
            AgentAudit::create([
                'name'     => $data['name'],
                'agent_id' => $agent['id'],
                'appid'    => $data['appid'],
                'remark'   => $data['remark'],
            ]);
            return json(['code' => 0, 'msg' => '操作成功', 'data' => ['close' => 1]]);
        }

        $image   = $this->request->domain() . "/static/common/img/shiliappid.png";
        $builder = new \app\common\controller\FormBuilder();
        $builder
            ->setModel($model)
            ->setUrl('form')
            ->setSuccessUrl()
            ->addFormItem('name', 'text', '微信小店名称')
            ->addFormItem('appid', 'text', '微信小店ID')
            ->addFormItem('tip', 'tip', '', "<a href='{$image}' target='_blank' style='color: red'>查看小店id获取位置</a>")
            ->addFormItem('remark', 'textarea', '申请备注')
            ->setFormData($info)
            ->returnForm();
        $this->assign('module', 'app');
        $this->assign('is_verify', 0);
        return $this->fetch('admin@common/form');
    }
}
